package com.example.back.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

public interface DrugOrderMapper {
    @SelectProvider(type = SqlBuilder.class, method = "DrugOrderListSQL")
    List<Map<String, Object>> DrugOrderList(@Param("orderno") String orderno, @Param("consignee") String consignee,
            @Param("consigneeTel") String consigneeTel, @Param("orderstatus") byte orderstatus,
            @Param("ltype") int ltype, @Param("skip") int skip, @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "DrugOrderCountSQL")
    int DrugOrderCount(@Param("orderno") String orderno, @Param("consignee") String consignee,
            @Param("consigneeTel") String consigneeTel, @Param("orderstatus") byte orderstatus,
            @Param("ltype") int ltype);

    class SqlBuilder {
        public String DrugOrderListSQL(@Param("orderno") String orderno, @Param("consignee") String consignee,
                @Param("consigneeTel") String consigneeTel, @Param("orderstatus") byte orderstatus,
                @Param("ltype") int ltype, @Param("skip") int skip, @Param("limit") int limit) {
            String sql = "with paget as (" + " select a.rx_order_no_nvarchar from order_rx_main a "
                    + " left join shop_order_logistics b on a.rx_order_no_nvarchar = b.order_no_nvarchar"
                    + " where 1=1";
            if (!orderno.isEmpty() && orderno.length() > 0)
                sql += " and (a.order_no_nvarchar like CONCAT('%',#{orderno},'%') or a.rx_order_no_nvarchar like CONCAT('%',#{orderno},'%'))";
            if (!consignee.isEmpty() && consignee.length() > 0)
                sql += " and b.name_nvarchar like CONCAT('%',#{consignee},'%')";
            if (!consigneeTel.isEmpty() && consigneeTel.length() > 0)
                sql += " and b.tel_nvarchar like CONCAT('%',#{consigneeTel},'%')";
            if (ltype != -1)
                sql += " and b.l_type_int = #{ltype} ";
            if (orderstatus != -1) {
                switch (orderstatus) {
                    case 1:
                        sql += " and a.system_status_tinyint = 1";
                        break;
                    case 2:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 0 and b.is_send_bit = 1 and b.is_arrive_bit = 0";
                        break;
                    case 3:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 1 and b.is_send_bit = 0";
                        break;
                    case 4:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 1 and b.is_send_bit = 1 and b.is_arrive_bit = 0";
                        break;
                    case 5:
                        sql += " and a.system_status_tinyint = 2 and b.is_send_bit = 1 and b.is_arrive_bit = 1";
                        break;
                    case 6:
                        sql += " and a.system_status_tinyint = 3";
                        break;
                    default:
                        break;
                }
            }
            sql += " order by a.create_time_datetime desc" + " limit #{skip},#{limit})"
                    + " select a.rx_order_no_nvarchar as RxOrderNO,a.order_no_nvarchar as OrderNO,a.create_time_datetime as CreateTime,"
                    + " a.order_pay_time_datetime as OrderPayTime,b.l_type_int as LType,a.sale_price_money as SalePrice,a.freight_money as Freight,"
                    + " cast(a.pay_type_tinyint as SIGNED INTEGER) as PayType,b.name_nvarchar as Name,b.tel_nvarchar as Tel,b.waybill_nvarchar as Waybill,"
                    + " cast(a.system_status_tinyint as SIGNED INTEGER) as SystemStatus,b.is_send_bit as IsSend,b.is_arrive_bit as IsArrive"
                    + " from paget p" + " inner join order_rx_main a on a.rx_order_no_nvarchar = p.rx_order_no_nvarchar"
                    + " left join shop_order_logistics b on a.rx_order_no_nvarchar = b.order_no_nvarchar";
            return sql;
        }

        public String DrugOrderCountSQL(@Param("orderno") String orderno, @Param("consignee") String consignee,
                @Param("consigneeTel") String consigneeTel, @Param("orderstatus") byte orderstatus,
                @Param("ltype") int ltype) {
            String sql = "select count(a.rx_order_no_nvarchar) from order_rx_main a "
                    + " left join shop_order_logistics b on a.rx_order_no_nvarchar = b.order_no_nvarchar"
                    + " where 1=1";
            if (!orderno.isEmpty() && orderno.length() > 0)
                sql += " and (a.order_no_nvarchar like CONCAT('%',#{orderno},'%') or a.rx_order_no_nvarchar like CONCAT('%',#{orderno},'%'))";
            if (!consignee.isEmpty() && consignee.length() > 0)
                sql += " and b.name_nvarchar like CONCAT('%',#{consignee},'%')";
            if (!consigneeTel.isEmpty() && consigneeTel.length() > 0)
                sql += " and b.tel_nvarchar like CONCAT('%',#{consigneeTel},'%')";
            if (ltype != -1)
                sql += " and b.l_type_int = #{ltype}";
            if (orderstatus != -1) {
                switch (orderstatus) {
                    case 1:
                        sql += " and a.system_status_tinyint = 1";
                        break;
                    case 2:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 0 and b.is_send_bit = 1 and b.is_arrive_bit = 0";
                        break;
                    case 3:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 1 and b.is_send_bit = 0";
                        break;
                    case 4:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 1 and b.is_send_bit = 1 and b.is_arrive_bit = 0";
                        break;
                    case 5:
                        sql += " and a.system_status_tinyint = 2 and b.l_type_int = 1 and b.is_send_bit = 1 and b.is_arrive_bit = 1";
                        break;
                    case 6:
                        sql += " and a.system_status_tinyint = 3";
                        break;
                    default:
                        break;
                }
            }
            return sql;
        }
    }
}
